/////////////////////////////////////////////////////////////////////////////
// Filename: Vector3.h
// Description: 3D vector class
/////////////////////////////////////////////////////////////////////////////

#ifndef VECTOR3_H
#define VECTOR3_H

#include "math.h"

class Vector3{
public:
	float x,y,z;

public:
	Vector3(float _x, float _y, float _z) {x=_x; y=_y; z=_z;}
	Vector3() {Vector3(0,0,0);}

	// [] operator to get elements
	float &operator [](int i) {return (&x)[i];}
	const float &operator [](int i) const {return (&x)[i];}

	// assignment operator
	Vector3  &operator=(const Vector3 &v) {x=v.x;y=v.y;z=v.z; return *this;}

	//relation operators
	bool operator ==(const Vector3 &p) const {return ((x==p.x)&&(y==p.y)&&(z==p.z));}
	bool operator !=(const Vector3 &p) const {return !((*this)==p);}

	//simple arithmetics
	Vector3  &operator+=(const Vector3 &p) {x+=p.x;y+=p.y;z+=p.z;return *this;}
	Vector3  operator + (const Vector3 &p) const {return Vector3(x+p.x,y+p.y,z+p.z);}

	Vector3  &operator-=(const Vector3 &p) {x-=p.x;y-=p.y;z-=p.z;return *this;}
	Vector3  operator - (const Vector3 &p) const {return Vector3(x-p.x,y-p.y,z-p.z);}
	Vector3  operator - () const {return Vector3(-x,-y,-z);}

	Vector3  &operator*=(float f) {x*=f;y*=f;z*=f;return *this;} 
	Vector3  operator * (float f) const {return Vector3(x*f,y*f,z*f);}

	Vector3  &operator/=(float f) {x/=f;y/=f;z/=f;return *this;}
	Vector3  operator / (float f) const {return Vector3(x/f,y/f,z/f);}

	//elementwise multiplication
	Vector3  mult_elem(const Vector3 &p) const { return Vector3(x*p.x,y*p.y,z*p.z);}

	//scalar product
	float  operator * (const Vector3 &v2) const {return x*v2.x+y*v2.y+z*v2.z;}

	//vector product
	Vector3  operator ^ (const Vector3 &v2) const {
		Vector3 tmp;
		tmp.x=y*v2.z-z*v2.y;
		tmp.y=z*v2.x-x*v2.z;
		tmp.z=x*v2.y-y*v2.x;
		return tmp;
	}

	//abs by definition
	float  abs() const {return sqrt((*this)*(*this));}

	//normalize to length one
	Vector3  &normalize() {return (*this)/=abs();}


};

//wrong order multiply
inline Vector3 operator *(float f, const Vector3 &v) {return v*f;}


#endif

/////////////////////////////////////////////////////////////////////////////
//                               END OF FILE                               //
/////////////////////////////////////////////////////////////////////////////